package com.mingdao.lhy.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mingdao.lhy.biz.WishFillingBiz;
import com.mingdao.lhy.pojo.ArtSpecialty;
import com.mingdao.lhy.pojo.ResponseVo;
import com.mingdao.lhy.pojo.SpecialProject;
import com.mingdao.lhy.pojo.Zkw;
import com.mingdao.lhy.pojo.page.ArtSpecialtySignUp;
import com.mingdao.lhy.pojo.page.ChangePassword;
import com.mingdao.lhy.service.StateService;
import com.mingdao.lhy.service.ZkwService;

@Controller
@RequestMapping("/state")
public class WishStateController {
	private static Logger log = LoggerFactory.getLogger(WishStateController.class);

	@Autowired
	private WishFillingBiz wishFillingBiz;

	@Autowired
	private ZkwService zkwService;

	@Autowired
	private StateService stateService;

	@RequestMapping("")
	public String index() {
		return "loginState";
	}

	/**
	 * 州教育局登录验证页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/check", method = RequestMethod.POST)
	public String check(@ModelAttribute("state") Zkw zkw, Model model) {

		HttpSession session = wishFillingBiz.getSession();

		log.debug("进入【州教育局登录验证页面】");
		if (null == zkw || null == zkw.getUserid() || zkw.getUserid().isEmpty()) {
			log.error("没有收到有效的请求数据");
			model.addAttribute("msg", "没有收到有效的请求数据");
			return "loginState";
		}
		/**
		 * 如果登录的用户ID不为空，并且不等于"71000001"，则不是州教育局身份
		 */
		if (null != zkw.getUserid() && !"71000001".equals(zkw.getUserid())) {
			log.error("只能是州教育局登录");
			model.addAttribute("msg", "只能是州教育局登录");
			return "loginState";
		}

		/**
		 * 根据用户名查询州教育局信息
		 */
		Zkw zkwDb = new Zkw();
		zkwDb = zkwService.getZkwById(zkw.getUserid());
		if (null == zkwDb) {
			log.error("没有找到指定的州教育局信息");
			model.addAttribute("msg", "没有找到指定的州教育局信息");
			return "loginState";
		}

		/**
		 * 判断州教育局是否第一次登录（如果密码为空，或者和州教育局代码相同则是初次登录）
		 * 
		 * 如果是，跳转到修改密码界面，如果不是，则继续前进到艺体特色报名审核页面
		 */
		String dbPassword = zkwDb.getPassword().trim();
		if (null == dbPassword || (dbPassword.equals(zkwDb.getUserid()) && dbPassword.equals(zkw.getPassword()))) {
			log.debug("州教育局初次登录 ，进入【密码修改页面】");
			model.addAttribute("zkw", zkwDb);
			session.setAttribute("zkw", zkwDb);
			model.addAttribute("changePassword", new ChangePassword());
			return "state/changePasswdState";

		}

		// 验证是否找到对应的州教育局
		// 并且输入的密码是否和查找到的州教育局密码是否一致
		if (null != zkwDb.getPassword() && zkwDb.getPassword().equals(zkw.getPassword())) {

			// 验证成功转到填报页面
			log.debug("验证【成功】");
			model.addAttribute("zkw", zkwDb);
			if (null == session) {
				session = wishFillingBiz.getSession();
			}
			session.setAttribute("zkw", zkwDb);

			// List<ArtSpecialtySignUp> students =
			// zkwService.getAllWishedStudentArtSpecialty(null);
			// model.addAttribute("students", students);

			// 转到艺体特色报名审核页面
			return "state/stateselect";
		} else {
			// 验证失败继续回到登录页面
			// Model model = new Model();
			log.debug("验证【失败】");

			model.addAttribute("msg", "验证失败，州教育局代码或密码不正确");
			return "loginState";
		}

	}

	/* 艺体审核 */
	@RequestMapping("showArtSpecialtyStateInit")
	public String showArtSpecialtyStateInit(Model model) {
		HttpSession session = wishFillingBiz.getSession();
		if (null == session || null == session.getAttribute("zkw")) {
			session = null;
			log.error("未登录，请重新登录");
			/* return "redirect:/loginStatEnrol"; */
		}
		Zkw zkw = (Zkw) session.getAttribute("zkw");
		model.addAttribute("zkw", zkw);
		List<ArtSpecialtySignUp> students = zkwService.getAllWishedStudentArtSpecialtyYt(null);
		model.addAttribute("students", students);

		List<ArtSpecialty> artSpecialtys = stateService.getArtSpecialty();
		model.addAttribute("artSpecialtys", artSpecialtys);

		return "state/wishFillingStateYt";

	}

	@RequestMapping("showWishArtSpecialty")
	@ResponseBody
	public List<ArtSpecialtySignUp> wish(@ModelAttribute("bmh") String bmh, Model model) {

		HttpSession session = wishFillingBiz.getSession();
		Zkw zkw = (Zkw) session.getAttribute("zkw");
		if (null == zkw || null == zkw.getUserid()) {
			log.error("没有收到有效的请求数据");
			model.addAttribute("msg", "没有收到有效的请求数据");
			// return "loginState";
		}
		List<ArtSpecialtySignUp> students = zkwService.getAllWishedStudentArtSpecialtyYt(bmh);
		model.addAttribute("students", students);
		return students;
		// 转到艺体特色报名审核页面
		// return "state/wishFillingStateTs";
	}

	/* 特色审核 */
	@RequestMapping("showArtSpecialtyStateTsInit")
	public String showArtSpecialtyStateTsInit(Model model) {
		HttpSession session = wishFillingBiz.getSession();
		if (null == session || null == session.getAttribute("zkw")) {
			session = null;
			log.error("未登录，请重新登录");
			/* return "redirect:/loginStatEnrol"; */
		}
		Zkw zkw = (Zkw) session.getAttribute("zkw");
		model.addAttribute("zkw", zkw);

		List<ArtSpecialtySignUp> students = zkwService.getAllWishedStudentArtSpecialtyTs(null);
		model.addAttribute("students", students);

		List<SpecialProject> specialProjects = stateService.getSpecialProject();
		model.addAttribute("specialProjects", specialProjects);

		return "state/wishFillingStateTs";

	}

	@RequestMapping("showWishArtSpecialtyTs")
	@ResponseBody
	public List<ArtSpecialtySignUp> wishTs(@ModelAttribute("bmh") String bmh, Model model) {

		HttpSession session = wishFillingBiz.getSession();
		Zkw zkw = (Zkw) session.getAttribute("zkw");
		if (null == zkw || null == zkw.getUserid()) {
			log.error("没有收到有效的请求数据");
			model.addAttribute("msg", "没有收到有效的请求数据");
			// return "loginState";
		}
		List<ArtSpecialtySignUp> students = zkwService.getAllWishedStudentArtSpecialtyTs(bmh);
		model.addAttribute("students", students);
		return students;
		// 转到艺体特色报名审核页面
		// return "state/wishFillingStateTs";
	}

	@RequestMapping("doSelectArtSpecialtyByBmhYt")
	public String selectArtSpecialtyByBmh(@ModelAttribute("bmh") String bmh, @ModelAttribute("sele") String sele,
			Model model) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		Zkw zkw = (Zkw) session.getAttribute("zkw");
		if (null == session || null == zkw) {
			session = null;
			log.error("州教育局未登录，请重新登录");
			vo.setCode("error");
			vo.setMessage("州教育局未登录，请重新登录");
			vo.setUrl("redirect:/loginState");
			// return vo;
		}

		if (null == bmh || bmh.isEmpty()) {
			vo.setCode("stay");
			vo.setMessage("未收到查询条件报名号");
			vo.setUrl("state/wishFillingStateYt");
			// return vo;
		}
		List<ArtSpecialtySignUp> students = new ArrayList<ArtSpecialtySignUp>();

		students = zkwService.getAllWishedStudentArtSpecialtyYt(bmh);

		model.addAttribute("students", students);
		model.addAttribute("zkw", zkw);
		model.addAttribute("sele", sele);

		// vo.setCode("ok");
		// vo.setMessage("查询完成");
		vo.setUrl("state/wishFillingStateYt");

		return "state/wishFillingStateYt";
	}

	@RequestMapping("doSelectArtSpecialtyByBmhTs")
	public String selectArtSpecialtyByBmhTs(@ModelAttribute("bmh") String bmh, @ModelAttribute("sele") String sele,
			Model model) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		Zkw zkw = (Zkw) session.getAttribute("zkw");
		if (null == session || null == zkw) {
			session = null;
			log.error("州教育局未登录，请重新登录");
			vo.setCode("error");
			vo.setMessage("州教育局未登录，请重新登录");
			vo.setUrl("redirect:/loginState");
			// return vo;
		}

		if (null == bmh || bmh.isEmpty()) {
			vo.setCode("stay");
			vo.setMessage("未收到查询条件报名号");
			vo.setUrl("state/wishFillingStateTs");
			// return vo;
		}
		List<ArtSpecialtySignUp> students = new ArrayList<ArtSpecialtySignUp>();

		students = zkwService.getAllWishedStudentArtSpecialtyTs(bmh);

		model.addAttribute("students", students);
		model.addAttribute("zkw", zkw);
		model.addAttribute("sele", sele);

		// vo.setCode("ok");
		// vo.setMessage("查询完成");
		vo.setUrl("state/wishFillingStateTs");

		return "state/wishFillingStateTs";
	}

	/**
	 * 根据高中学校查询艺体特色报名学生信息**
	 * 
	 * @param highSchoolId
	 * @param model
	 * @return
	 */
	@RequestMapping("doSelectArtSpecialtyByHighSchoolYt")
	@ResponseBody
	public List<ArtSpecialtySignUp> selectArtSpecialtyByHighSchoolYt(
			@ModelAttribute("highSchoolId") String highSchoolId, Model model) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		Zkw zkw = (Zkw) session.getAttribute("zkw");
		if (null == session || null == zkw) {
			session = null;
			log.error("州教育局未登录，请重新登录");
			vo.setCode("error");
			vo.setMessage("州教育局未登录，请重新登录");
			vo.setUrl("redirect:/loginState");
			// return vo;
		}

		if (null == highSchoolId || highSchoolId.isEmpty()) {
			vo.setCode("stay");
			vo.setMessage("未收到查询条件高中学校ID");
			vo.setUrl("state/wishFillingStateTs");
			// return vo;
		}
		List<ArtSpecialtySignUp> students = new ArrayList<ArtSpecialtySignUp>();

		students = zkwService.getAllWishedStudentArtSpecialtyByHighSchoolYt(highSchoolId);

		model.addAttribute("students", students);
		model.addAttribute("zkw", zkw);

		// vo.setCode("ok");
		// vo.setMessage("查询完成");
		vo.setUrl("state/wishFillingStateTs");

		// return "state/wishFillingStateTs";
		return students;
	}

	/**
	 * 根据高中学校查询特色报名学校信息
	 * 
	 * @param highSchoolId
	 * @param model
	 * @return
	 */
	@RequestMapping("doSelectArtSpecialtyByHighSchoolTs")
	@ResponseBody
	public List<ArtSpecialtySignUp> selectArtSpecialtyByHighSchoolTs(
			@ModelAttribute("highSchoolId") String highSchoolId, Model model) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		Zkw zkw = (Zkw) session.getAttribute("zkw");
		if (null == session || null == zkw) {
			session = null;
			log.error("州教育局未登录，请重新登录");
			vo.setCode("error");
			vo.setMessage("州教育局未登录，请重新登录");
			vo.setUrl("redirect:/loginState");
			// return vo;
		}

		if (null == highSchoolId || highSchoolId.isEmpty()) {
			vo.setCode("stay");
			vo.setMessage("未收到查询条件高中学校ID");
			vo.setUrl("state/wishFillingStateTs");
			// return vo;
		}
		List<ArtSpecialtySignUp> students = new ArrayList<ArtSpecialtySignUp>();

		students = zkwService.getAllWishedStudentArtSpecialtyByHighSchoolTs(highSchoolId);

		model.addAttribute("students", students);
		model.addAttribute("zkw", zkw);

		// vo.setCode("ok");
		// vo.setMessage("查询完成");
		vo.setUrl("state/wishFillingStateTs");

		// return "state/wishFillingStateTs";
		return students;
	}

	/**
	 * 单个提交审核状态（艺体特）
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "doSaveCheckResult", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo saveScore(@ModelAttribute("row") ArtSpecialtySignUp artSpecialtySignUp, Model model) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		Zkw zkw = (Zkw) session.getAttribute("zkw");
		if (null == session || null == zkw) {
			session = null;
			log.error("州教育局未登录，请重新登录");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("州教育局未登录，请重新登录");
			vo.setUrl("redirect:/loginState");
			return vo;
		}
		// String row = (String) model.asMap().get("row");
		/**
		 * 后台数据库保存审核状态
		 */
		ArtSpecialtySignUp artSpecialtySignUpTmp = new ArtSpecialtySignUp();
		artSpecialtySignUpTmp.setBmh(artSpecialtySignUp.getBmh());
		artSpecialtySignUpTmp.setZjwshzt(artSpecialtySignUp.getZjwshzt());
		int result = zkwService.updateArtSpecialtyShztYt(artSpecialtySignUpTmp);
		if (result != 1) {
			log.error("提交审核状态失败");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("提交审核状态失败");
			vo.setUrl("state/wishFillingStateYt");
			// return vo;
		} else {
			vo.setCode("ok");
			vo.setMessage("提交成绩成功");
			vo.setUrl("state/wishFillingStateYt");
		}
		// return "highschool/wishFillingHighSchoolTs";
		return vo;
	}

	/**
	 * 单个提交审核状态（特色项目）
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "doSaveCheckResultTs", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo saveScoreTs(@ModelAttribute("row") ArtSpecialtySignUp artSpecialtySignUp, Model model) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		Zkw zkw = (Zkw) session.getAttribute("zkw");
		if (null == session || null == zkw) {
			session = null;
			log.error("州教育局未登录，请重新登录");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("州教育局未登录，请重新登录");
			vo.setUrl("redirect:/loginState");
			return vo;
		}
		// String row = (String) model.asMap().get("row");
		/**
		 * 后台数据库保存审核状态
		 */
		ArtSpecialtySignUp artSpecialtySignUpTmp = new ArtSpecialtySignUp();
		artSpecialtySignUpTmp.setBmh(artSpecialtySignUp.getBmh());
		artSpecialtySignUpTmp.setZjwshzt(artSpecialtySignUp.getZjwshzt());
		int result = zkwService.updateArtSpecialtyShztTs(artSpecialtySignUpTmp);
		if (result != 1) {
			log.error("提交审核状态失败");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("提交审核状态失败");
			vo.setUrl("state/wishFillingStateTs");
			// return vo;
		} else {
			vo.setCode("ok");
			vo.setMessage("提交成绩成功");
			vo.setUrl("state/wishFillingStateTs");
		}
		// return "highschool/wishFillingHighSchoolTs";
		return vo;
	}

	/**
	 * 批量审核（艺体特长）
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "doExamineAll", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo examine(Model model, @RequestParam("bmh") String bmh, @RequestParam("zjwshzt") String zjwshzt) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		Zkw zkw = (Zkw) session.getAttribute("zkw");
		if (null == session || null == zkw) {
			session = null;
			log.error("州教育局未登录，请重新登录");
			vo.setCode("error");
			vo.setMessage("州教育局未登录，请重新登录");
			vo.setUrl("redirect:/loginState");
			return vo;
		}
		/**
		 * 后台数据库保存审核状态
		 */
		ArtSpecialtySignUp artSpecialtySignUp = new ArtSpecialtySignUp();
		artSpecialtySignUp.setBmh(bmh);
		artSpecialtySignUp.setZjwshzt(zjwshzt);
		int result = zkwService.updateArtSpecialtyShztYt(artSpecialtySignUp);
		if (result != 1) {
			log.error("审核状态更新失败");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("审核状态更新失败");
			vo.setUrl("state/wishFillingStateYt");
			return vo;
		}
		vo.setCode("ok");
		vo.setMessage("审核状态更新成功");
		vo.setUrl("state/wishFillingStateYt");
		// return "highschool/wishFillingHighSchoolTs";
		return vo;
	}

	/**
	 * 批量审核（特色项目）
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "doExamineAllTs", method = RequestMethod.POST)
	@ResponseBody
	public ResponseVo examineTs(Model model, @RequestParam("bmh") String bmh, @RequestParam("zjwshzt") String zjwshzt) {
		ResponseVo vo = new ResponseVo();
		HttpSession session = wishFillingBiz.getSession();
		Zkw zkw = (Zkw) session.getAttribute("zkw");
		if (null == session || null == zkw) {
			session = null;
			log.error("州教育局未登录，请重新登录");
			vo.setCode("error");
			vo.setMessage("州教育局未登录，请重新登录");
			vo.setUrl("redirect:/loginState");
			return vo;
		}
		/**
		 * 后台数据库保存审核状态
		 */
		ArtSpecialtySignUp artSpecialtySignUp = new ArtSpecialtySignUp();
		artSpecialtySignUp.setBmh(bmh);
		artSpecialtySignUp.setZjwshzt(zjwshzt);
		int result = zkwService.updateArtSpecialtyShztTs(artSpecialtySignUp);
		if (result != 1) {
			log.error("审核状态更新失败");
			// return "redirect:/loginHighSchool";
			vo.setCode("error");
			vo.setMessage("审核状态更新失败");
			vo.setUrl("state/wishFillingStateTs");
			return vo;
		}
		vo.setCode("ok");
		vo.setMessage("审核状态更新成功");
		vo.setUrl("state/wishFillingStateTs");
		// return "highschool/wishFillingHighSchoolTs";
		return vo;
	}

	@RequestMapping(value = "doChangePasswd", method = RequestMethod.POST)
	public String changePwd(@ModelAttribute(value = "changePassword") ChangePassword changePassword, Model model) {

		HttpSession session = wishFillingBiz.getSession();
		Zkw zkw = (Zkw) session.getAttribute("zkw");
		if (null == session || null == zkw) {
			session = null;
			log.error("用户未登录，请重新登录");
			return "redirect:/state/loginState";
		}
		/**
		 * 判断传入的密码
		 */
		ChangePassword cp = (ChangePassword) model.asMap().get("changePassword");
		if (null == model || null == model.asMap() || null == cp) {
			log.error("未取到前台返回的密码值 ");
			model.addAttribute("msg", "未取到前台返回的密码值");
			// model.addAttribute("student", student);
			return "state/changePasswdState";
		}

		if (null == cp.getOldPassword()) {
			log.error("必须输入原来的密码值 ");
			model.addAttribute("msg", "必须输入原来的密码值");
			// model.addAttribute("student", student);
			return "state/check";
		}
		if (null == cp.getNewPasswordOne() || null == cp.getNewPasswordTwo()) {
			log.error("必须输入新的码值 ");
			model.addAttribute("msg", "必须输入新的码值");
			// model.addAttribute("student", student);
			return "state/changePasswdState";
		}

		String oldPassword = cp.getOldPassword();
		String newPasswordOne = cp.getNewPasswordOne();
		String newPasswordTwo = cp.getNewPasswordTwo();

		/**
		 * 如果输入的旧密码等于州教育局本来的旧密码才可以更换密码
		 */
		if (!oldPassword.isEmpty() && !zkw.getPassword().isEmpty()) {
			if (oldPassword.equals(zkw.getPassword())) {
				/**
				 * 两次输入密码要一致才有效
				 */
				if (newPasswordOne.equals(newPasswordTwo)) {
					zkw.setPassword(newPasswordOne);
				} else {
					log.error("两次密码不符，请重新核对 ");
					model.addAttribute("msg", "两次密码不符，请重新核对 ");
					model.addAttribute("zkw", zkw);
					return "state/changePasswdState";
				}
			} else {
				log.error("原密码不符，请重新核对 ");
				model.addAttribute("msg", "原密码不符，请重新核对 ");
				model.addAttribute("zkw", zkw);
				return "state/changePasswdState";
			}
		}

		/**
		 * 修改密码
		 */
		int result = zkwService.changePassword(zkw);
		if (1 == result) {
			log.debug("密码修改成功");
		} else {
			log.warn("密码修改失败");
			model.addAttribute("zkw", zkw);
			return "state/changePasswdState";
		} /**
			 * 跳转到登录界面重新登录
			 */
		return "loginState";
	}

	@RequestMapping("forgetPwd")
	public String forgetPwd() {
		return "forgetPasswd";
	}
}
